import 'package:animation_test/pages/gallery_detail_page.dart';
import 'package:flutter/cupertino.dart';

class GalleryFrameAnimationPainter extends CustomPainter {
  final List<LightPoint> frameData;
  final int imageWidth;
  final int imageHeight;

  GalleryFrameAnimationPainter(
      this.imageWidth, this.imageHeight, this.frameData);

  @override
  void paint(Canvas canvas, Size size) {

    double offsetX = 0;
    double offsetY = 0;

    double pointSize = 1;

    double scaleValue = 1;

    int displayWidth = size.width.toInt();
    int displayHeight = size.height.toInt();
    double displayRatio = displayWidth / displayHeight;

    double imageRatio = imageWidth / imageHeight;

    if (displayRatio < imageRatio) {
      scaleValue = size.width / imageWidth;
      offsetY = (displayHeight - displayWidth / imageRatio) * 0.5;
    } else if (displayRatio > imageRatio) {
      scaleValue = size.height / imageHeight;
      offsetX = (displayWidth - displayHeight * imageRatio) * 0.5;
    } else {
      scaleValue = size.width / imageWidth;
    }

    for (int i = 0; i < frameData.length; i++) {
      LightPoint lightPoint = frameData[i];
      var dx = lightPoint.x * scaleValue - pointSize * 0.5 + offsetX;
      var dy = lightPoint.y * scaleValue - pointSize * 0.5 + offsetY;
      canvas.drawRect(
          Rect.fromPoints(
              Offset(dx, dy), Offset(dx + pointSize, dy + pointSize)),
          Paint()
            ..style = PaintingStyle.fill
            ..color = lightPoint.color);
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}
